<?php

use app\widgets\PartialActiveForm;
use app\models\FunctionPointExt;
use yii\helpers\Url;
use yii\helpers\Html;

function formatList($arr = [])
{
	return array_combine($arr, $arr);
}
$form = PartialActiveForm::begin(['id' => 'formAddFunctionPoint']);
?>

<?= Html::hiddenInput('id', $funpoint->id) ?>
<?= Html::hiddenInput('FunctionPoint[parent_id]', $funpoint->parent_id); ?>
<?= $form->field($funpoint, 'name') ?>
<?= $form->field($funpoint, 'description')->textarea() ?>
<?= $form->field($funpoint, 'controller')->dropDownList(formatList(FunctionPointExt::getControllerList()), ['prompt' => '----请选择----', 'onChange' => 'onChangeController(this)']) ?>
<?= $form->field($funpoint, 'action')->dropDownList(formatList(FunctionPointExt::getActionList($funpoint->controller)), ['prompt' => '----请选择----']) ?>

<?php $form::end() ?>
<script type="text/javascript">
	function onChangeController(obj)
	{
		var controller = $(obj).val();
		requestJson('<?= Url::toRoute("system/action-list") ?>', {controller: controller}, true, function (res) {
			// 生成选项
			if (res.status == 0) {
				var action = $('select#functionpoint-action');
				action.empty();
				action.append('<option value="">----请选择----</option>');
				// 不能使用for in
				$.each(res.data, function(index, val) {
					action.append('<option value="' + val + '">' + val + '</option>');
				});
			} else {
				history.go(0);
			}
		});
	}
</script>
